{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1.13 \u901a\u8fc7\u67d0\u4e2a\u5173\u952e\u5b57\u6392\u5e8f\u4e00\u4e2a\u5b57\u5178\u5217\u8868\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\u5217\u8868\uff0c\u4f60\u60f3\u6839\u636e\u67d0\u4e2a\u6216\u67d0\u51e0\u4e2a\u5b57\u5178\u5b57\u6bb5\u6765\u6392\u5e8f\u8fd9\u4e2a\u5217\u8868\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u4f7f\u7528 operator \u6a21\u5757\u7684 itemgetter \u51fd\u6570\uff0c\u53ef\u4ee5\u975e\u5e38\u5bb9\u6613\u7684\u6392\u5e8f\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002\n\u5047\u8bbe\u4f60\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u51fa\u6765\u7f51\u7ad9\u4f1a\u5458\u4fe1\u606f\u5217\u8868\uff0c\u5e76\u4e14\u4ee5\u4e0b\u5217\u7684\u6570\u636e\u7ed3\u6784\u8fd4\u56de\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "rows = [\n    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},\n    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},\n    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},\n    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6839\u636e\u4efb\u610f\u7684\u5b57\u5178\u5b57\u6bb5\u6765\u6392\u5e8f\u8f93\u5165\u7ed3\u679c\u884c\u662f\u5f88\u5bb9\u6613\u5b9e\u73b0\u7684\uff0c\u4ee3\u7801\u793a\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from operator import itemgetter\nrows_by_fname = sorted(rows, key=itemgetter('fname'))\nrows_by_uid = sorted(rows, key=itemgetter('uid'))\nprint(rows_by_fname)\nprint(rows_by_uid)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ee3\u7801\u7684\u8f93\u51fa\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'},\n{'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'},\n{'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},\n{'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]\n[{'fname': 'John', 'uid': 1001, 'lname': 'Cleese'},\n{'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},\n{'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'},\n{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "itemgetter() \u51fd\u6570\u4e5f\u652f\u6301\u591a\u4e2a keys\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u4ee3\u7801"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "rows_by_lfname = sorted(rows, key=itemgetter('lname','fname'))\nprint(rows_by_lfname)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f1a\u4ea7\u751f\u5982\u4e0b\u7684\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "[{'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},\n{'fname': 'John', 'uid': 1001, 'lname': 'Cleese'},\n{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'},\n{'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c rows \u88ab\u4f20\u9012\u7ed9\u63a5\u53d7\u4e00\u4e2a\u5173\u952e\u5b57\u53c2\u6570\u7684 sorted() \u5185\u7f6e\u51fd\u6570\u3002\n\u8fd9\u4e2a\u53c2\u6570\u662f callable \u7c7b\u578b\uff0c\u5e76\u4e14\u4ece rows \u4e2d\u63a5\u53d7\u4e00\u4e2a\u5355\u4e00\u5143\u7d20\uff0c\u7136\u540e\u8fd4\u56de\u88ab\u7528\u6765\u6392\u5e8f\u7684\u503c\u3002\nitemgetter() \u51fd\u6570\u5c31\u662f\u8d1f\u8d23\u521b\u5efa\u8fd9\u4e2a callable \u5bf9\u8c61\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "operator.itemgetter() \u51fd\u6570\u6709\u4e00\u4e2a\u88ab rows \u4e2d\u7684\u8bb0\u5f55\u7528\u6765\u67e5\u627e\u503c\u7684\u7d22\u5f15\u53c2\u6570\u3002\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u5178\u952e\u540d\u79f0\uff0c\n\u4e00\u4e2a\u6574\u5f62\u503c\u6216\u8005\u4efb\u4f55\u80fd\u591f\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\u7684 __getitem__() \u65b9\u6cd5\u7684\u503c\u3002\n\u5982\u679c\u4f60\u4f20\u5165\u591a\u4e2a\u7d22\u5f15\u53c2\u6570\u7ed9 itemgetter() \uff0c\u5b83\u751f\u6210\u7684 callable \u5bf9\u8c61\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5143\u7d20\u503c\u7684\u5143\u7ec4\uff0c\n\u5e76\u4e14 sorted() \u51fd\u6570\u4f1a\u6839\u636e\u8fd9\u4e2a\u5143\u7ec4\u4e2d\u5143\u7d20\u987a\u5e8f\u53bb\u6392\u5e8f\u3002\n\u4f46\u4f60\u60f3\u8981\u540c\u65f6\u5728\u51e0\u4e2a\u5b57\u6bb5\u4e0a\u9762\u8fdb\u884c\u6392\u5e8f\uff08\u6bd4\u5982\u901a\u8fc7\u59d3\u548c\u540d\u6765\u6392\u5e8f\uff0c\u4e5f\u5c31\u662f\u4f8b\u5b50\u4e2d\u7684\u90a3\u6837\uff09\u7684\u65f6\u5019\u8fd9\u79cd\u65b9\u6cd5\u662f\u5f88\u6709\u7528\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "itemgetter() \u6709\u65f6\u5019\u4e5f\u53ef\u4ee5\u7528 lambda \u8868\u8fbe\u5f0f\u4ee3\u66ff\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "rows_by_fname = sorted(rows, key=lambda r: r['fname'])\nrows_by_lfname = sorted(rows, key=lambda r: (r['lname'],r['fname']))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6848\u4e5f\u4e0d\u9519\u3002\u4f46\u662f\uff0c\u4f7f\u7528 itemgetter() \u65b9\u5f0f\u4f1a\u8fd0\u884c\u7684\u7a0d\u5fae\u5feb\u70b9\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5bf9\u6027\u80fd\u8981\u6c42\u6bd4\u8f83\u9ad8\u7684\u8bdd\u5c31\u4f7f\u7528 itemgetter() \u65b9\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u4e0d\u8981\u5fd8\u4e86\u8fd9\u8282\u4e2d\u5c55\u793a\u7684\u6280\u672f\u4e5f\u540c\u6837\u9002\u7528\u4e8e min() \u548c max() \u7b49\u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min(rows, key=itemgetter('uid'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "max(rows, key=itemgetter('uid'))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}